package org.geemp.game.controller;

import org.geemp.game.chart.service.ChartService;
import org.geemp.game.common.CommonResult;
import org.geemp.game.gm.service.GameDBServiceImpl;
import org.geemp.game.service.BaseDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author JackRen
 * @date 2021-12-03 13:55
 * @description:
 */
@RestController
public class WebUpController {

    @Autowired
    private GameDBServiceImpl gameDBService;

    @Autowired
    private ChartService chartService;

    @Autowired
    private BaseDBService baseDBService;



    @GetMapping(value = "/webUp/testJson")
    public Object testJson() {
        return CommonResult.success(chartService.obtainItemNameBy("31"));
    }


    /*
     * 根据session中的用户名和角色名获取charIndex
     * @author JackRen
     * @date 2021/12/3
     * @return
     **/

    private String ObtainChartIndex(HttpServletRequest request) {
        HttpSession session =request.getSession();
        if (session.getAttribute("ut_userid")==null || session.getAttribute("ut_role")==null) {
            return "";
        }
        String CT_USERID = (String) session.getAttribute("ut_userid");
        String CT_NAME = (String) session.getAttribute("ut_role");
        Map<String,Object> map = gameDBService.obtainChartIndexBy(CT_USERID,CT_NAME);
        if (map==null){
            return "";
        }
        return String.valueOf(map.get("CT_SEQ"));
    }


    /*
     * 根据IT_SLOTTYPE和IT_USERIDX获取IT_ITEMIDX
     * @author JackRen
     * @date 2021/12/3
     * @return
     **/
    private Map<String,Object> obtainInChantIDBy(HttpServletRequest request,String IT_SLOTTYPE) {
        String IT_USERIDX = ObtainChartIndex(request);
        if (StringUtils.isEmpty(IT_USERIDX)){
            return null;
        }

        Map<String,Object> map = gameDBService.obtainITEMIDXBy(IT_USERIDX,IT_SLOTTYPE);
        if (map==null){
            return null;
        }
        return map;
    }


    /*
     * 根据IT_ITEMIDX获取IC_ITEMNAME
     * @author JackRen
     * @date 2021/12/3
     * @return
     **/

    private String obtainIcItemNameBy(String IT_ITEMIDX) {
        if (StringUtils.isEmpty(IT_ITEMIDX)){
            return "";
        }

        Map<String,Object> map = chartService.obtainItemNameBy(IT_ITEMIDX);
        if (map==null){
            return "";
        }
        return String.valueOf(map.get("IC_ITEMNAME"));
    }


    @RequestMapping(value = "/webUp/obtainItemName",method = RequestMethod.POST)
    public Object obtainItemName(HttpServletRequest request,String IT_SLOTTYPE) {
        Map<String,Object> map= obtainInChantIDBy(request,IT_SLOTTYPE);
        if (StringUtils.isEmpty(String.valueOf(map.get("IT_ITEMIDX")))) {
            return CommonResult.failed("无法找到角色名！");
        }

        String IC_ITEMNAME = obtainIcItemNameBy(String.valueOf(map.get("IT_ITEMIDX")));
        if (StringUtils.isEmpty(IC_ITEMNAME)) {
            return CommonResult.failed("无法找到该物品！");
        }
        map.put("IC_ITEMNAME",IC_ITEMNAME);
        return CommonResult.success(map) ;
    }

    /*
     * 根据IT_SEQ更新INVENTPORYTABLE表
     * @author JackRen
     * @date 2021/12/6
     * @return
     **/
    @RequestMapping(value = "/webUp/updateItemInfoByID",method = RequestMethod.POST)
    public Object updateItemInfoByID(int IT_SEQ, int IT_OPTIONID1,int IT_OPTIONVALUE1,
                                     int IT_OPTIONID2, int IT_OPTIONVALUE2,
                                     int IT_OPTIONID3, int IT_OPTIONVALUE3,
                                     int IT_OPTIONID4, int IT_OPTIONVALUE4,
                                     int IT_OPTIONID5, int IT_OPTIONVALUE5,
                                     int IT_OPTIONID6, int IT_OPTIONVALUE6,
                                     int IT_OPTIONID7, int IT_OPTIONVALUE7,int CT_GOLD,HttpServletRequest request) {
        HttpSession session =request.getSession();
        if (session.getAttribute("ut_userid")==null || session.getAttribute("ut_role")==null) {
            return CommonResult.failed("请先登录用户");
        }
        String CT_USERID = (String) session.getAttribute("ut_userid");
        int ret = sysChartInfo(IT_SEQ,CT_GOLD,CT_USERID);
        if (ret ==-1) {
            return CommonResult.failed("金币不足");
        }

        int result = gameDBService.updateItemInfoByID(IT_SEQ,IT_OPTIONID1,IT_OPTIONVALUE1,IT_OPTIONID2,IT_OPTIONVALUE2,
                IT_OPTIONID3,IT_OPTIONVALUE3,IT_OPTIONID4,IT_OPTIONVALUE4,IT_OPTIONID5,IT_OPTIONVALUE5,
                IT_OPTIONID6,IT_OPTIONVALUE6,IT_OPTIONID7,IT_OPTIONVALUE7);
        if (result<=0) {
            return CommonResult.failed("数据更新失败！！！");
        }
        return CommonResult.success("数据更新成功！！！") ;
    }

    private int sysChartInfo(int IT_SEQ,int CT_GOLD,String CT_USERID) {
        return gameDBService.updateCTChartInfo(IT_SEQ,CT_GOLD,CT_USERID);
    }

    @RequestMapping(value = "/webUp/obtainUpGradeCharInfoMaps",method = RequestMethod.GET)
    public Object obtainUpGradeCharInfoMaps(String type) {
        List<Map<String,Object>> mapList = gameDBService.obtainUpGradeCharInfoMaps(type);
        return CommonResult.success(mapList) ;
    }


    @RequestMapping(value = "/webUp/obtainSolyTypeBy",method = RequestMethod.POST)
    public Object obtainSolyTypeBy(HttpServletRequest request,String IT_SLOTTYPE) {
        String IT_USERIDX = ObtainChartIndex(request);
        if (StringUtils.isEmpty(IT_USERIDX)){
            return null;
        }

        Map<String,Object> map = gameDBService.obtainITEMIDXBy(IT_USERIDX,IT_SLOTTYPE);
        if (map==null){
            return CommonResult.failed("无装备") ;
        }
        return CommonResult.success(map) ;
    }

}
